package com.vue.vuedemo.controller;

import com.vue.vuedemo.annotation.CheckToken;
import com.vue.vuedemo.map.ImgaesMapper;
import com.vue.vuedemo.pojo.Images;
import com.vue.vuedemo.viewpojo.ResList;
import com.vue.vuedemo.vo.CommentVo;
import com.vue.vuedemo.vo.ImagesInfoVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import java.util.Arrays;
import java.util.List;

@RestController
public class ImagesController {
    @Autowired
    ImgaesMapper imgaesMapper;
    
    @CheckToken
    //根据分类id查图片
    @GetMapping("/getimages/{id}")
    public ResList<Images> getImages(HttpServletResponse response,@PathVariable("id") Integer id){
        response.setHeader("Access-Control-Allow-Origin", "*");
        ResList<Images> list = new ResList<Images>();
        list.setStatus(0);
        
        if(id==0){
            List<Images> allImages = imgaesMapper.getAllImages();
            list.setMessage(allImages);
            return list;
        }else {
            List<Images> imagesByCid = imgaesMapper.getImagesByCid(id);
            list.setMessage(imagesByCid);
            
            return list;
        }

    }
    
    @CheckToken
    //根据图片id查详情
    @GetMapping("/getimageInfo/{id}")
    public ResList<ImagesInfoVo> getimageInfo(HttpServletResponse response,@PathVariable("id") Integer id){
        response.setHeader("Access-Control-Allow-Origin", "*");
        Images imagesById = imgaesMapper.getImagesById(id);
        
        ImagesInfoVo imagesInfoVo = new ImagesInfoVo();
        BeanUtils.copyProperties(imagesById, imagesInfoVo);
        
        ResList<ImagesInfoVo> resList = new ResList();
        resList.setStatus(0);
        resList.setMessage(Arrays.asList(imagesInfoVo));
        
        
        return resList;
    }
}
